import simplejson
import traceback

from django.http import HttpResponse
from django.contrib.csrf.middleware import csrf_exempt

from musiclink.models import MidLink

@csrf_exempt
def anno_newid(request):
    """
    POST method

    : params k :
    : params type :
    : params newid :

    : return
    """
    try:
        k = request.POST['k']
        id_type = request.POST['type']
        newid = request.POST['newid']
        ml = MidLink.get(k)
        if id_type == "xid":
            ml.xid = int(newid)
        elif id_type == "bid":
            ml.bid = int(newid)
        elif id_type == "kid":
            ml.kid = int(newid)
        elif id_type == "mid":
            ml.mid = int(newid)
        else:
            raise Exception('no such type')
        ml.put()
        return HttpResponse(simplejson.dumps({'code':0}))
    except:
        return HttpResponse(simplejson.dumps({
            'code':1,
            'data':{
                'errorMessage':traceback.format_exc()
                }}))

